package z_mxsTest.testECUConfig;

import ma.xin.shi.ConfigurationManager;
import ma.xin.shi.configurations.ECU.ECUConfig;
import ma.xin.shi.configurations.ECU.ECUEntry;

import java.util.List;

public class ECUConfigTest {
    public static void main(String[] args) {
        ConfigurationManager configManager = ConfigurationManager.getInstance();
        ECUConfig ecuConfig = configManager.getECUConfig();

        // === Step 1: 创建空 Entry 并添加到 Config ===
        ECUEntry entry = new ECUEntry();  // 所有字段初始化为空或默认值
        entry.setEcuName("ECU01");       // 唯一标识符必须先设好
        ecuConfig.addECU(entry);

        System.out.println("【After Add】Empty ECU added:");
        printECU(ecuConfig.getECUByName("ECU01"));

        // === Step 2: 逐步修改字段 ===
        ECUEntry loadedEntry = ecuConfig.getECUByName("ECU01");
        if (loadedEntry != null) {
            loadedEntry.setMacAddress("00:1A:2B:3C:4D:5E");
            loadedEntry.setInitDelayMin("50");
            loadedEntry.setInitDelayMax("150");
            loadedEntry.setRepetitionBaseDelay("100");
            loadedEntry.setRepetitionsMax("3");
            loadedEntry.setCyclicOfferDelay("1000");
            loadedEntry.setRequestResponseDelayMin("20");
            loadedEntry.setRequestResponseDelayMax("80");
            loadedEntry.setRemark("Initial configuration");

            System.out.println("\n【After Update】Fields updated:");
            printECU(loadedEntry);
        }

        // === Step 3: 查询所有条目 ===
        System.out.println("\n【Final State】All ECUs:");
        printAllECUs(ecuConfig);

        // === Step 4: 删除条目 ===
        ecuConfig.removeECU("ECU01");
        System.out.println("\n【After Delete】ECU01 removed.");

        // 验证是否删除成功
        if (ecuConfig.getECUByName("ECU01") == null) {
            System.out.println("ECU01 has been successfully deleted.");
        }
    }

    private static void printECU(ECUEntry entry) {
        if (entry == null) {
            System.out.println("ECU not found.");
            return;
        }
        System.out.println(entry.toString());
    }

    private static void printAllECUs(ECUConfig ecuConfig) {
        List<ECUEntry> entries = ecuConfig.getAllECUs();
        if (entries.isEmpty()) {
            System.out.println("No ECUs configured.");
        } else {
            for (ECUEntry entry : entries) {
                System.out.println(entry);
            }
        }
    }
}
